package com.commerce.product.controller;

import com.commerce.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 秒杀监控控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/flash-sale/monitor")
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
public class FlashSaleMonitorController {

    /**
     * 获取实时数据
     */
    @GetMapping("/realtime")
    public Result<Map<String, Object>> getRealTimeData() {
        log.info("获取实时监控数据");
        
        Map<String, Object> data = new HashMap<>();
        data.put("onlineUsers", 1250);
        data.put("currentQPS", 850);
        data.put("successRate", 98.5);
        data.put("avgResponseTime", 120);
        
        return Result.success(data);
    }

    /**
     * 获取热门商品
     */
    @GetMapping("/hot-products")
    public Result<List<Map<String, Object>>> getHotProducts() {
        log.info("获取热门商品数据");
        
        List<Map<String, Object>> products = new ArrayList<>();
        
        Map<String, Object> product1 = new HashMap<>();
        product1.put("id", 1);
        product1.put("name", "iPhone 15 Pro");
        product1.put("requests", 5200);
        product1.put("successRate", 95.8);
        product1.put("trend", 1);
        products.add(product1);
        
        Map<String, Object> product2 = new HashMap<>();
        product2.put("id", 2);
        product2.put("name", "MacBook Air M2");
        product2.put("requests", 3800);
        product2.put("successRate", 97.2);
        product2.put("trend", 1);
        products.add(product2);
        
        Map<String, Object> product3 = new HashMap<>();
        product3.put("id", 3);
        product3.put("name", "AirPods Pro");
        product3.put("requests", 2900);
        product3.put("successRate", 99.1);
        product3.put("trend", -1);
        products.add(product3);
        
        return Result.success(products);
    }

    /**
     * 获取系统状态
     */
    @GetMapping("/system-status")
    public Result<Map<String, String>> getSystemStatus() {
        log.info("获取系统状态");
        
        Map<String, String> status = new HashMap<>();
        status.put("redis", "正常");
        status.put("database", "正常");
        status.put("mq", "正常");
        
        return Result.success(status);
    }

    /**
     * 获取系统指标
     */
    @GetMapping("/system-metrics")
    public Result<List<Map<String, Object>>> getSystemMetrics() {
        log.info("获取系统指标");
        
        List<Map<String, Object>> metrics = new ArrayList<>();
        
        Map<String, Object> metric1 = new HashMap<>();
        metric1.put("metric", "CPU使用率");
        metric1.put("current", "45%");
        metric1.put("threshold", "80%");
        metric1.put("status", "正常");
        metric1.put("description", "系统CPU使用率正常");
        metrics.add(metric1);
        
        Map<String, Object> metric2 = new HashMap<>();
        metric2.put("metric", "内存使用率");
        metric2.put("current", "62%");
        metric2.put("threshold", "85%");
        metric2.put("status", "正常");
        metric2.put("description", "系统内存使用率正常");
        metrics.add(metric2);
        
        Map<String, Object> metric3 = new HashMap<>();
        metric3.put("metric", "数据库连接数");
        metric3.put("current", "25");
        metric3.put("threshold", "100");
        metric3.put("status", "正常");
        metric3.put("description", "数据库连接池正常");
        metrics.add(metric3);
        
        Map<String, Object> metric4 = new HashMap<>();
        metric4.put("metric", "Redis连接数");
        metric4.put("current", "15");
        metric4.put("threshold", "50");
        metric4.put("status", "正常");
        metric4.put("description", "Redis连接池正常");
        metrics.add(metric4);
        
        return Result.success(metrics);
    }

    /**
     * 获取告警日志
     */
    @GetMapping("/alert-logs")
    public Result<List<Map<String, Object>>> getAlertLogs() {
        log.info("获取告警日志");
        
        List<Map<String, Object>> logs = new ArrayList<>();
        
        Map<String, Object> log1 = new HashMap<>();
        log1.put("time", LocalDateTime.now().minusMinutes(5));
        log1.put("level", "警告");
        log1.put("message", "QPS超过阈值，当前值：1200");
        log1.put("source", "商品服务");
        logs.add(log1);
        
        Map<String, Object> log2 = new HashMap<>();
        log2.put("time", LocalDateTime.now().minusMinutes(15));
        log2.put("level", "信息");
        log2.put("message", "系统负载恢复正常");
        log2.put("source", "监控系统");
        logs.add(log2);
        
        return Result.success(logs);
    }

    /**
     * 获取QPS数据
     */
    @GetMapping("/qps-data")
    public Result<Map<String, Object>> getQPSData(@RequestParam(defaultValue = "1h") String timeRange) {
        log.info("获取QPS数据，时间范围：{}", timeRange);
        
        Map<String, Object> data = new HashMap<>();
        
        // 生成模拟时间序列
        List<String> times = new ArrayList<>();
        List<Integer> values = new ArrayList<>();
        
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
        LocalDateTime now = LocalDateTime.now();
        
        for (int i = 59; i >= 0; i--) {
            times.add(now.minusMinutes(i).format(formatter));
            // 生成模拟QPS数据
            values.add(500 + (int)(Math.random() * 400));
        }
        
        data.put("times", times);
        data.put("values", values);
        
        return Result.success(data);
    }

    /**
     * 获取响应时间数据
     */
    @GetMapping("/response-time-data")
    public Result<Map<String, Object>> getResponseTimeData(@RequestParam(defaultValue = "1h") String timeRange) {
        log.info("获取响应时间数据，时间范围：{}", timeRange);
        
        Map<String, Object> data = new HashMap<>();
        
        // 生成模拟时间序列
        List<String> times = new ArrayList<>();
        List<Integer> values = new ArrayList<>();
        
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
        LocalDateTime now = LocalDateTime.now();
        
        for (int i = 59; i >= 0; i--) {
            times.add(now.minusMinutes(i).format(formatter));
            // 生成模拟响应时间数据
            values.add(80 + (int)(Math.random() * 60));
        }
        
        data.put("times", times);
        data.put("values", values);
        
        return Result.success(data);
    }

    /**
     * 获取错误率数据
     */
    @GetMapping("/error-rate-data")
    public Result<List<Map<String, Object>>> getErrorRateData() {
        log.info("获取错误率数据");
        
        List<Map<String, Object>> data = new ArrayList<>();
        
        Map<String, Object> item1 = new HashMap<>();
        item1.put("name", "成功");
        item1.put("value", 95);
        data.add(item1);
        
        Map<String, Object> item2 = new HashMap<>();
        item2.put("name", "库存不足");
        item2.put("value", 3);
        data.add(item2);
        
        Map<String, Object> item3 = new HashMap<>();
        item3.put("name", "系统错误");
        item3.put("value", 1);
        data.add(item3);
        
        Map<String, Object> item4 = new HashMap<>();
        item4.put("name", "网络超时");
        item4.put("value", 1);
        data.add(item4);
        
        return Result.success(data);
    }
}
